package com.youka.bussiness.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youka.bussiness.common.utils.BaseUtil;
import com.youka.bussiness.dao.entity.Products;
import com.youka.bussiness.service.ProductsService;
import com.youka.bussiness.service.req.ProductsReqDto;
import com.youka.bussiness.service.req.ProductsUpdateDto;
import com.youka.bussiness.service.req.ProductsPageDto;
import com.youka.bussiness.service.res.ProductsDto;;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youka.bussiness.common.bean.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.youka.bussiness.dao.ProductsMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 产品信息表 Service 实现类
 *
 * @author lfc
 * @date 2024-06-30 16:47:28
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {

    @Resource
    private ProductsMapper productsMapper;

    /**
     * 查询详情
     *
     * @param id 查询参数
     * @return productsDto
     */
    @Override
    public ProductsDto selectById(long id) {
        ProductsDto productsDto = new ProductsDto();
        Products products = productsMapper.selectById(id);
        if (products != null){
            BeanUtils.copyProperties(products,productsDto);
        }
        return productsDto;
    }

    /**
     * 集合查询产品信息表
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public List<ProductsDto> selectByList(ProductsReqDto dto) {
        LambdaQueryWrapper<Products> wrapper = new LambdaQueryWrapper();
        List<Products> Productss = productsMapper.selectList(wrapper);
        return BaseUtil.listToList(Productss, ProductsDto.class);
    }

    /**
     * 分页查询产品信息表
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public IPage selectByPage(ProductsPageDto dto) {
        QueryWrapper<Products> wrapper = Page.getPageWrapper(dto);
        return Page.copyProperties(this.page(Page.defaultPage(dto.getPageIndex(), dto.getPageSize()), wrapper)
                ,ProductsDto.class);
    }

    /**
     * 新增产品信息表
     *
     * @param dto 产品信息表
     * @return 新增结果
     */
    @Override
    public void insert(ProductsReqDto dto) {
        Products products = new Products();
        BeanUtils.copyProperties(dto,products);
        productsMapper.insert(products);
    }

    /**
       * 统计
        * @param dto
       * @return
       */
    @Override
     public  ProductsDto selectCount(ProductsReqDto dto){
        ProductsDto resp = new ProductsDto();
        productsMapper.selectCount(new LambdaQueryWrapper<Products>());
        return resp;
    }

    /**
     * 更新产品信息表
     * (根据主键)
     *
     * @param dto 产品信息表
     * @return 更新结果
     */
    @Override
    public void update(ProductsUpdateDto dto) {
        Products products =new Products();
        BeanUtils.copyProperties(dto,products);
        productsMapper.updateById(products);
    }

    /**
     * 删除产品信息表
     * (根据主键)
     *
     * @param id id
     * @return 删除结果
     */
    @Override
    public void deleteById(Integer id) {
        productsMapper.deleteById(id);
    }


}
